var requestUrl = "http://applog.szjs.gov.cn/elasticsearch/_msearch";
var xhr = new XMLHttpRequest();
var sendData = function () {
    xhr.open("POST", requestUrl);
    xhr.setRequestHeader("kbn-version", "6.4.2");
    xhr.setRequestHeader("Content-Type", "application/x-ndjson");
    var log = "";
    var recorded = false;
    var hits = null;
    // xhr.overrideMimeType("application/x-ndjson")
    xhr.onreadystatechange = function (e) {
        if (this.readyState == 4 && this.status == 200) {
            var resp = this.responseText;
            var respData = JSON.parse(resp);
            hits = respData && respData.responses && respData.responses[0] && respData.responses[0].hits && respData.responses[0].hits.hits;
            if (hits instanceof Array) {
                for (var i = 0; i < hits.length; i++) {
                    var item = hits[i];
                    log += ("\n" + item._source.message);
                }
                recorded == true;
            }
            console.log(log);
        }
    };
    // 查询index配置
    var indexConfig = {
        "index": "applog-*",
        "ignore_unavailable": true,
        "timeout": 30000,
        "preference": 1600765917567
    };
    var queryConfig = {
        "version": true,
        "size": 5000,
        "sort": [{
            "@timestamp": {
                "order": "desc",
                "unmapped_type": "boolean"
            }
        }],
        "_source": {
            "excludes": []
        },
        "aggs": {
            "2": {
                "date_histogram": {
                    "field": "@timestamp",
                    "interval": "5s",
                    "time_zone": "Asia/Shanghai",
                    "min_doc_count": 1
                }
            }
        },
        "stored_fields": ["*"],
        "script_fields": {},
        "docvalue_fields": [{ "field": "@timestamp", "format": "date_time" }],
        "query": {
            "bool": {
                "must": [{
                    "match_all": {}
                }, {
                    "match_phrase": {
                        "source": {

                            "query": "/home/weblogic/log/zfxx_fygl.log"
                        }
                    }
                }, {
                    "bool": {
                        "should": [{
                            "match_phrase": {
                                "beat.hostname": "zfxx-fygl1"
                            }
                        }, {
                            "match_phrase": {
                                "beat.hostname": "zfxx-fygl2"
                            }
                        }],
                        "minimum_should_match": 1
                    }
                }, {
                    "range": {
                        "@timestamp": {
                            "gte": 1600765586489,
                            "lte": 1600765935934,
                            "format": "epoch_millis"
                        }
                    }
                }],
                "filter": [],
                "should": [],
                "must_not": []
            }
        },
        "highlight": {
            "pre_tags": ["@kibana-highlighted-field@"],
            "post_tags": ["@/kibana-highlighted-field@"],
            "fields": {
                "*": {}
            },
            "fragment_size": 2147483647
        }
    }
    // 查询配置
    var requestParam = JSON.stringify(indexConfig) + "\n" + JSON.stringify(queryConfig) + "\n";
    xhr.send(requestParam);
}
sendData();